<?php

namespace app\admin\model;

use app\admin\model\InsurancePolicy;
use app\common\constants\MenuConstant;
use app\common\model\TimeModel;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
// use think\crypt\Crypt;
use app\common\utils\Crypt;
use think\model\relation\BelongsTo;
use think\model\relation\HasMany;

class InsuranceUser extends TimeModel
{

    public function policy(): HasMany{
        return $this->hasMany(InsurancePolicy::class,'user_id','id');
    }

    protected function getOptions(): array
    {
        return [
            'deleteTime' => 'delete_time',
        ];
    }

    public function setIdcardAttr($value){
        return empty($value) ? $value : Crypt::encrypt($value);
    }

    public function getIdcardAttr($value){
        return empty($value) ? $value : Crypt::decrypt($value);
    }

    public function setCarnoAttr($value){
        return empty($value) ? $value : Crypt::encrypt($value);
    }

    public function getCarnoAttr($value){
        return empty($value) ? $value : Crypt::decrypt($value);
    }

    public function setMobileAttr($value){
        return empty($value) ? $value : Crypt::encrypt($value);
    }

    public function getMobileAttr($value){
        return empty($value) ? $value : Crypt::decrypt($value);
    }

    /**
     * @throws ModelNotFoundException
     * @throws DbException
     * @throws DataNotFoundException
     */
    public static function getList(): array
    {
        $field = 'id,insurer,carno';
        $list = self::field($field)->where([
            ['status', '=', 1],
        ])->select()->toArray();

        return $list;
    }

}